package com.jinlejia.feedback.question.adapter;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfListMode;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.feedback.R;
import com.jinlejia.feedback.databinding.FbAdapterAllProgressBinding;
import com.jinlejia.feedback.weight.FbDownloadFileView;
import com.weight.CircleProgressBar;

import java.util.ArrayList;
import java.util.Map;

/**
 * 全部跟进 适配器
 */

public class AllProgressAdapter extends YfListAdapter<Map<String, Object>>{

    public AllProgressAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_adapter_all_progress, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_empty_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fb_loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder){
            bindCurrViewHolder((ViewHolder) holder,position);
        }
    }
    public interface DownloadEvent{
        void onDownload(CircleProgressBar progressBar,String downloadUrl);
    }

    private DownloadEvent downloadEvent;

    public void setDownloadEvent(DownloadEvent downloadEvent) {
        this.downloadEvent = downloadEvent;
    }

    private void bindCurrViewHolder(ViewHolder holder, int position){
        FbAdapterAllProgressBinding binding = DataBindingUtil.getBinding(holder.itemView);
        Map<String, Object> map = mData.get(position);
        binding.setMap(map);
        //跟进内容 之前的空格是为了让`跟进`标签能进行正确展示
        String content = "              "+String.valueOf(map.get("followContent"));
        binding.noteTextDetailsView.setText(content);
        //region 附件列表

        ArrayList<Map<String,String>> files = (ArrayList<Map<String, String>>) map.get("fileList");
        Context context = holder.itemView.getContext();
        if (files!=null && files.size()>0){
            binding.fileDownloadLayout.setVisibility(View.VISIBLE);
            binding.fileDownloadLayout.removeAllViews();
            for (int i=0;i<files.size();i++) {
                Map<String,String> file = files.get(i);
                FbDownloadFileView view = new FbDownloadFileView(context);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
                params.width = LinearLayout.LayoutParams.MATCH_PARENT;
                params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
                if (i==0){
                    params.topMargin = dip2px(context,12);
                }else {
                    params.topMargin = dip2px(context,7F);
                }

                params.bottomMargin = dip2px(context,7F);
                params.leftMargin = dip2px(context,6);
                params.rightMargin = dip2px(context,6);
                if (i==file.size()-1){
                    params.bottomMargin = dip2px(context,11);
                }
                view.setLayoutParams(params);
                view.setFileName(file.get("fileName"));
                view.setDownloadUrl(file.get("fileUrl"));
                view.setDownloadListener(new FbDownloadFileView.DownloadListener() {
                    @Override
                    public void download(CircleProgressBar progressBar, String downloadUrl) {
                        downloadEvent.onDownload(progressBar,downloadUrl);
                    }
                });
                binding.fileDownloadLayout.addView(view);
            }
        }else {
            binding.fileDownloadLayout.setVisibility(View.GONE);
        }


        //endregion
    }

    private final int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    @Override
    public int getItemViewType(int position) {
        if (mData==null || mData.isEmpty())return super.getItemViewType(position);
        return YfListMode.MODE_DATA;
    }


    @Override
    public int getItemCount() {
        if (mData==null || mData.isEmpty())return 1;
        return super.getItemCount();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        public ViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }
}
